package com.ly.hotel.view.setting;

import java.util.List;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.ly.hotel.R;
import com.ly.hotel.msgevent.CloseMsgEvent;
import com.ly.hotel.msgevent.GetGameEvent;
import com.ly.hotel.msgevent.ShowMsgEvent;

import de.greenrobot.event.EventBus;

public class MyHome extends Activity {
	private List<ResolveInfo> mApps;
	GridView mGrid;
	private OnItemClickListener listener = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			ResolveInfo info = mApps.get(position);

			// 该应用的包名
			String pkg = info.activityInfo.packageName;
			// 应用的主activity类
			String cls = info.activityInfo.name;

			ComponentName componet = new ComponentName(pkg, cls);

			Intent i = new Intent();
			i.setComponent(componet);
			startActivity(i);
		}

	};

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		EventBus.getDefault().register(this);
		loadApps();
		setContentView(R.layout.activity_myhome);
		mGrid =  findViewById(R.id.apps_list);

		AppsAdapter mAdapter = new AppsAdapter(this);
		mGrid.setAdapter(mAdapter);
		mGrid.setOnItemClickListener(listener);
		Log.i("myhome", "onCreate");
	}

	@Override
	protected void onResume() {
		super.onResume();
		EventBus.getDefault().post(new CloseMsgEvent());
	}

	@Override
	protected void onDestroy() {
		Log.i("myhome", "onDestroy");
		EventBus.getDefault().post(new ShowMsgEvent());
		EventBus.getDefault().unregister(this);
		super.onDestroy();
	}
	/* 游戏相关的数据获取事件接口 */
	public void onEventMainThread(GetGameEvent event) {

	}
	private void loadApps() {
		Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
		mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

		mApps = getPackageManager().queryIntentActivities(mainIntent, 0);
	}

	public class AppsAdapter extends BaseAdapter {
		Context mContext;
		LayoutInflater mInflater;

		public AppsAdapter(Context c) {
			mContext = c;
			mInflater = LayoutInflater.from(mContext);
		}

		public View getView(int position, View contentView, ViewGroup parent) {
			ViewHolder holder;
			if (contentView == null) {
				holder = new ViewHolder();
				contentView = mInflater.inflate(R.layout.myhome_gridview_item, null);
				holder.mImg = contentView.findViewById(R.id.mAppsImage);
				ResolveInfo info = mApps.get(position);
				PackageManager packageManager = mContext.getPackageManager();
				holder.mImg.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));
				holder.mTxt = contentView.findViewById(R.id.mAppsName);
				holder.mTxt.setText(info.loadLabel(packageManager).toString());
			} else {
				holder = (ViewHolder) contentView.getTag();
			}
			contentView.setTag(holder);
			return contentView;
		}

		public final int getCount() {
			return mApps.size();
		}

		public final Object getItem(int position) {
			return mApps.get(position);
		}

		public final long getItemId(int position) {
			return position;
		}

		class ViewHolder {
			ImageView mImg;
			TextView mTxt;
		}
	}
}